'''
Created on Aug 20, 2009

@author: echeek
'''

import string

numerals={"I":1,
          "V":5,
          "X":10,
          "L":50,
          "C":100,
          "D":500,
          "M":1000,
          "IV":4,
          "IX":9,
          "XL":40,
          "XC":90,
          "CD":400,
          "CM":900,
          }

def romanToInteger(roman):
    value=0;
    curtotal=0;
    curchar='I';
    for c in roman:
        if(curchar != c):
            if(numerals[curchar]<numerals[c]):
                value-=curtotal;
            else:
                value+=curtotal;
            curtotal=0;
            curchar=c;
        curtotal+=numerals[c];
    value+=curtotal;
    return value;

def toMiniRoman(value):
    roman='';
    while value>0:
        maxc='I';
        maxv=1;
        for c,v in numerals.items():
            if(v<=value and v>maxv):
                maxv=v;
                maxc=c;
        value-=maxv;
        roman+=maxc;
    return roman;


def Euler89():
    file=open('Euler89data.txt', 'r');
    total=0;
    line=file.readline();
    while line!='' and line!=None:
        
        #print romanToInteger(line[:-1]);
        total+=(len(line)-1)-len( toMiniRoman(romanToInteger(line[:-1])));
        line=file.readline();
    print total;    
Euler89();
        
        
#print toMiniRoman(4330);
#print toMiniRoman(969);